target:=apk
target:=ipa
target:=exe

dev_mobileprovision:= FamilyTree_Dev01.mobileprovision
dev_p12:= develop.p12
release_mobileprovision:= FamilyTree_Product01.mobileprovision
release_p12:= product_02.p12
ios_pswd:=11111111



ifeq ($(OS),Windows_NT)
	HOME:=d:
	ndk_home:=$(HOME)/android-ndk-r8e
	flex_home:=$(HOME)/air28
	gcc_os:=windows
	#^"<> must convert to ^^ ^" ^< ^>
#	caret:=^
else
	OS:=$(shell uname)
ifeq ($(OS),Darwin)
	flex_home:=$(HOME)/air_sdk
	ndk_home:=$(HOME)/android-ndk-r9c
	gcc_os:=darwin-x86
	echo:=echo
else
	flex_home:=$(HOME)/flex_sdk_4.6
	ndk_home:=$(HOME)/android-ndk-r10
	gcc_os:=linux-x86
	echo:=echo -e
endif
endif
GCC:=$(ndk_home)/toolchains/arm-linux-androideabi-4.6/prebuilt/$(gcc_os)/bin/arm-linux-androideabi-gcc
sysroot:=$(ndk_home)/platforms/android-9/arch-arm/usr
include:= --sysroot $(sysroot) -I"$(flex_home)/include" -I"$(sysroot)/include"
library:= $(flex_home)/lib/android/FlashRuntimeExtensions.so

ifeq ($(OS),Darwin)
ifeq ($(target),ipa)
#ios_simulator_sdk:=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk
ios_simulator_sdk:=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
GCC:=$(shell xcodebuild -find clang)
min_ver:=7.0
include:=-g -O2 --sysroot $(ios_simulator_sdk) -miphoneos-version-min=$(min_ver) -I$(ios_simulator_sdk)/usr/include/ -I"$(flex_home)/include"
library:=-L$(ios_simulator_sdk)/usr/lib/
endif
endif
ifeq ($(target),exe)
GCC:=gcc
include:=-I"$(flex_home)/include"
library:= $(flex_home)/lib/win/FlashRuntimeExtensions.lib
library:= -L"$(flex_home)/lib/win" -lFlashRuntimeExtensions
endif

cflags:=-Wall -g $(include) $(library)

android_so:=libane.so
win_dll:=ane.dll
ios_a:=libane.a
library_swf:=library.swf
src:= ane.c
objs:= $(patsubst %.c,%.o,$(src))
adt:=java -Dflexlib="$(flex_home)/frameworks" -jar "$(flex_home)/lib/adt.jar"
adt:=$(flex_home)/bin/adt


acompc:=$(flex_home)/bin/acompc
amxmlc:=$(flex_home)/bin/amxmlc
adl:=$(flex_home)/bin/adl
apk:=test.apk
ipa:=test.ipa
exe:=test.exe
ane_id:=libiao.ane
ane_swc:=ane.swc
cert:=my.p12
cert_pswd:=test
android_ane_xml:=android-ane.xml
win_ane_xml:=win-ane.xml
ios_ane_xml:=ios-ane.xml
app_xml:=app.xml
app_name:=test
swf:=test.swf
app_id:=test
app_ver:=1.0
ane_ver:=1.0
as:=test.as
ane_class:=ane
android_ane:=android.ane
win_ane:=win.ane
ios_ane:=ios.ane

objs_armv7:=libv7.a
objs_armv7s:=libv7s.a
objs_arm64:=libv64.a
ios_x86:=libx86.a

icon:=	-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*icon[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
	-e 's/$(caret)<image16x16$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image16x16$(caret)>/$(caret)<image16x16$(caret)>icon\/16.png$(caret)<\/image16x16$(caret)>/' \
	-e 's/$(caret)<image32x32$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image32x32$(caret)>/$(caret)<image32x32$(caret)>icon\/32.png$(caret)<\/image32x32$(caret)>/' \
	-e 's/$(caret)<image36x36$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image36x36$(caret)>/$(caret)<image36x36$(caret)>icon\/36.png$(caret)<\/image36x36$(caret)>/' \
	-e 's/$(caret)<image48x48$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image48x48$(caret)>/$(caret)<image48x48$(caret)>icon\/48.png$(caret)<\/image48x48$(caret)>/' \
	-e 's/$(caret)<image57x57$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image57x57$(caret)>/$(caret)<image57x57$(caret)>icon\/57.png$(caret)<\/image57x57$(caret)>/' \
	-e 's/$(caret)<image72x72$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image72x72$(caret)>/$(caret)<image72x72$(caret)>icon\/72.png$(caret)<\/image72x72$(caret)>/' \
	-e 's/$(caret)<image114x114$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image114x114$(caret)>/$(caret)<image114x114$(caret)>icon\/114.png$(caret)<\/image114x114$(caret)>/' \
	-e 's/$(caret)<image128x128$(caret)>[$(caret)^$(caret)<]*$(caret)<\/image128x128$(caret)>/$(caret)<image128x128$(caret)>icon\/128.png$(caret)<\/image128x128$(caret)>/'

icon:=

all:$(target)
	@echo $(target) ok

ipa:$(ipa)

apk:$(apk)

exe:$(exe)

install:
ifeq ($(target),apk)
	adb install -r $(apk) && adb shell am start -a android.intent.action.MAIN -n air.$(app_id)/.AppEntry && adb logcat air.$(app_id):I ActivityManager:I *:S
endif


$(ios_a): $(objs_armv7) $(objs_armv7s) $(objs_arm64)
#armv7 armv7s arm64 i386
	lipo -create $(objs_armv7) $(objs_armv7s) $(objs_arm64) -output $@

$(objs_armv7):$(src)
	$(GCC) $(cflags) -arch armv7 -c $< -o libv7.o
	ar rcs $@ libv7.o

$(objs_armv7s):$(src)
	$(GCC) $(cflags) -arch armv7s -c $< -o libv7s.o
	ar rcs $@ libv7s.o

$(objs_arm64):$(src)
	$(GCC) $(cflags) -arch arm64 -c $< -o libv64.o
	ar rcs $@ libv64.o

$(ios_x86):$(src)
	$(GCC) $(cflags) -arch x86_64 -c $< -o libx86.o
	ar rcs $@ libx86.o
#arch = x86_64, i386, arm, thumb, mips, 

#	ar rcs $@ $^

$(android_so): $(objs)
	$(GCC) $(cflags) -shared $^ -o $@

$(win_dll): $(objs)
	$(GCC) -shared $^ -o $@ $(cflags)

#$(android_so): $(src)
#	clang -fobjc-arc -framework Foundation test.m -o test
#	$(GCC)  $(include) $(library) -g -Wall -shared $(src) -o $@
#	lipo -info lib_all.a 

.c.o:
	$(GCC) $(cflags) -c $<

$(ane_swc):
	$(acompc) -source-path . -include-classes $(ane_class) -swf-version=14 -output $(ane_swc)

$(ane_class): $(ane_class.as)


$(library_swf):
	@unzip $(ane_swc) library.swf -d .

$(win_ane): $(win_ane_xml) $(win_dll) $(ane_swc) $(library_swf)
	$(adt) -package -target ane $(win_ane) $(win_ane_xml) -swc $(ane_swc) -platform Windows-x86 $(win_dll) library.swf

$(android_ane): $(android_ane_xml) $(android_so) $(ane_swc) $(library_swf)
	$(adt) -package -target ane $(android_ane) $(android_ane_xml) -swc $(ane_swc) -platform Android-ARM $(android_so) library.swf

$(ios_ane): $(ios_ane_xml) $(ios_a) $(ane_swc) $(library_swf) # $(ios_x86)
	$(adt) -package -target ane $(ios_ane) $(ios_ane_xml) -swc $(ane_swc) -platform iPhone-ARM $(ios_a) library.swf
#	$(adt) -package -target ane $(ios_ane) $(ios_ane_xml) -swc $(ane_swc) -platform iPhone-x86 $(ios_x86) library.swf -platformsdk $(ios_simulator_sdk)
#iPhone-x86 Windows-x86 MacOS-x86 iPhone-ARM Android-ARM


$(swf): *.as $(ane_swc)
	$(amxmlc) -strict=true  -omit-trace-statements=false -verbose-stacktraces=false -source-path=. -debug=true $(as) -library-path+=$(ane_swc) -output $(swf)


$(app_xml):
	@sed $(icon) -e 's/$(caret)<id$(caret)>[$(caret)^$(caret)<]*$(caret)<\/id$(caret)>/$(caret)<id$(caret)>$(app_id)$(caret)<\/id$(caret)>/' \
		-e 's/$(caret)<content$(caret)>[$(caret)^$(caret)<]*$(caret)<\/content$(caret)>/$(caret)<content$(caret)>$(swf)$(caret)<\/content$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<name$(caret)>[$(caret)^$(caret)<]*$(caret)<\/name$(caret)>[$(caret)^a-z]*/$(caret)<name$(caret)>$(app_name)$(caret)<\/name$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<versionNumber$(caret)>[$(caret)^$(caret)<]*$(caret)<\/versionNumber$(caret)>[$(caret)^a-z]*/$(caret)<versionNumber$(caret)>$(app_ver)$(caret)<\/versionNumber$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<filename$(caret)>[$(caret)^$(caret)<]*$(caret)<\/filename$(caret)>[$(caret)^a-z]*/$(caret)<filename$(caret)>$(app_name)$(caret)<\/filename$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<extension$(caret)>[$(caret)^$(caret)<]*$(caret)<\/extension$(caret)>[$(caret)^a-z]*/$(caret)<extension$(caret)>extension$(caret)<\/extension$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<contentType$(caret)>[$(caret)^$(caret)<]*$(caret)<\/contentType$(caret)>[$(caret)^a-z]*/$(caret)<contentType$(caret)>contentType$(caret)<\/contentType$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<description$(caret)>[$(caret)^$(caret)<]*$(caret)<\/description$(caret)>[$(caret)^a-z]*/$(caret)<description$(caret)>description$(caret)<\/description$(caret)>/' \
		-e 's/$(caret)<\/id$(caret)>/$(caret)<\/id$(caret)>$(caret)<extensions$(caret)>$(caret)<extensionID$(caret)>$(ane_id)$(caret)<\/extensionID$(caret)>$(caret)<\/extensions$(caret)>/' \
		-e 's/$(caret)<!-- $(caret)<visible$(caret)>$(caret)<\/visible$(caret)> --$(caret)>/$(caret)<visible$(caret)>true$(caret)<\/visible$(caret)>/' \
		-e 's/$(caret)<!-- $(caret)<aspectRatio$(caret)>[$(caret)^$(caret)<]*$(caret)<\/aspectRatio$(caret)> --$(caret)>/$(caret)<aspectRatio$(caret)>portrait$(caret)<\/aspectRatio$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<manifest android:installLocation=$(caret)"auto$(caret)"$(caret)>/$(caret)<manifest android:installLocation=$(caret)"auto$(caret)"$(caret)> \
		$(caret)<uses-permission android:name=$(caret)"android.permission.ACCESS_NETWORK_STATE$(caret)" \/$(caret)> \
		$(caret)<uses-permission android:name=$(caret)"android.permission.ACCESS_WIFI_STATE$(caret)" \/$(caret)> \
		$(caret)<uses-permission android:name=$(caret)"android.permission.INTERNET$(caret)" \/$(caret)> \
		$(caret)<uses-permission android:name=$(caret)"android.permission.WRITE_EXTERNAL_STORAGE$(caret)" \/$(caret)>/' \
		-e 's/$(caret)<!-- $(caret)<supportedProfiles$(caret)>[$(caret)^$(caret)<]*$(caret)<\/supportedProfiles$(caret)> --$(caret)>/$(caret)<supportedProfiles$(caret)>desktop extendedDesktop mobileDevice extendedMobileDevice$(caret)<\/supportedProfiles$(caret)>/' \
		-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*android[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
		-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*manifestAdditions[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
		-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*fileTypes[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
		-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*fileType[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
		-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*iPhone[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
		-e 's/[$(caret)^$(caret)>]*\($(caret)<[\/]*InfoAdditions[$(caret)^$(caret)>]*$(caret)>\)[$(caret)^$(caret)<]*/\1/' \
		$(flex_home)/templates/air/descriptor-template.xml > $(app_xml)
  
$(android_ane_xml):
	@sed -e 's/$(caret)<id$(caret)>[$(caret)^$(caret)<]*$(caret)<\/id$(caret)>/$(caret)<id$(caret)>$(ane_id)$(caret)<\/id$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<versionNumber$(caret)>[$(caret)^$(caret)<]*$(caret)<\/versionNumber$(caret)>[$(caret)^a-z]*/$(caret)<versionNumber$(caret)>$(ane_ver)$(caret)<\/versionNumber$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<nativeLibrary$(caret)>[$(caret)^$(caret)<]*$(caret)<\/nativeLibrary$(caret)>[$(caret)^a-z]*/$(caret)<nativeLibrary$(caret)>$(android_so)$(caret)<\/nativeLibrary$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<initializer$(caret)>[$(caret)^$(caret)<]*$(caret)<\/initializer$(caret)>[$(caret)^a-z]*/$(caret)<initializer$(caret)>ExtInitializer$(caret)<\/initializer$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<finalizer$(caret)>[$(caret)^$(caret)<]*$(caret)<\/finalizer$(caret)>[$(caret)^a-z]*/$(caret)<finalizer$(caret)>ExtFinalizer$(caret)<\/finalizer$(caret)>/' \
		-e 's/$(caret)<platform name=$(caret)"[$(caret)^$(caret)"]*$(caret)"$(caret)>/$(caret)<platform name=$(caret)"Android-ARM$(caret)"$(caret)>/' \
		$(flex_home)/templates/extensions/extension-descriptor-template.xml > $(android_ane_xml)

$(ios_ane_xml):
	@sed -e 's/$(caret)<id$(caret)>[$(caret)^$(caret)<]*$(caret)<\/id$(caret)>/$(caret)<id$(caret)>$(ane_id)$(caret)<\/id$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<versionNumber$(caret)>[$(caret)^$(caret)<]*$(caret)<\/versionNumber$(caret)>[$(caret)^a-z]*/$(caret)<versionNumber$(caret)>$(ane_ver)$(caret)<\/versionNumber$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<nativeLibrary$(caret)>[$(caret)^$(caret)<]*$(caret)<\/nativeLibrary$(caret)>[$(caret)^a-z]*/$(caret)<nativeLibrary$(caret)>$(ios_a)$(caret)<\/nativeLibrary$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<initializer$(caret)>[$(caret)^$(caret)<]*$(caret)<\/initializer$(caret)>[$(caret)^a-z]*/$(caret)<initializer$(caret)>ExtInitializer$(caret)<\/initializer$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<finalizer$(caret)>[$(caret)^$(caret)<]*$(caret)<\/finalizer$(caret)>[$(caret)^a-z]*/$(caret)<finalizer$(caret)>ExtFinalizer$(caret)<\/finalizer$(caret)>/' \
		-e 's/$(caret)<platform name=$(caret)"[$(caret)^$(caret)"]*$(caret)"$(caret)>/$(caret)<platform name=$(caret)"iPhone-ARM$(caret)"$(caret)>/' \
		$(flex_home)/templates/extensions/extension-descriptor-template.xml > $(ios_ane_xml)

$(win_ane_xml):
	sed -e 's/$(caret)<id$(caret)>[$(caret)^$(caret)<]*$(caret)<\/id$(caret)>/$(caret)<id$(caret)>$(ane_id)$(caret)<\/id$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<versionNumber$(caret)>[$(caret)^$(caret)<]*$(caret)<\/versionNumber$(caret)>[$(caret)^a-z]*/$(caret)<versionNumber$(caret)>$(ane_ver)$(caret)<\/versionNumber$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<nativeLibrary$(caret)>[$(caret)^$(caret)<]*$(caret)<\/nativeLibrary$(caret)>[$(caret)^a-z]*/$(caret)<nativeLibrary$(caret)>$(win_dll)$(caret)<\/nativeLibrary$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<initializer$(caret)>[$(caret)^$(caret)<]*$(caret)<\/initializer$(caret)>[$(caret)^a-z]*/$(caret)<initializer$(caret)>ExtInitializer$(caret)<\/initializer$(caret)>/' \
		-e 's/$(caret)^[$(caret)^a-z]*$(caret)<finalizer$(caret)>[$(caret)^$(caret)<]*$(caret)<\/finalizer$(caret)>[$(caret)^a-z]*/$(caret)<finalizer$(caret)>ExtFinalizer$(caret)<\/finalizer$(caret)>/' \
		-e 's/$(caret)<platform name=$(caret)"[$(caret)^$(caret)"]*$(caret)"$(caret)>/$(caret)<platform name=$(caret)"Windows-x86$(caret)"$(caret)>/' \
		$(flex_home)/templates/extensions/extension-descriptor-template.xml > $(win_ane_xml)

$(exe): $(win_ane) $(cert) $(swf) $(app_xml)
	@unzip $(win_ane) -d w.ane
	$(adl) $(app_xml) -extdir . -profile extendedDesktop
#	$(adt) -package -storetype pkcs12 -keystore $(cert) -storepass $(cert_pswd) -target native $(exe) $(app_xml) $(swf) -extdir .
#	$(adt) -package -storetype pkcs12 -keystore $(cert) -storepass $(cert_pswd) -target bundle $(exe) $(app_xml) $(swf) -extdir .

$(apk): $(android_ane) $(cert) $(swf) $(app_xml)
#	$(adl) $(app_xml) -extdir . -profile extendedMobileDevice
	$(adt) -package -target apk-debug -storetype pkcs12 -keystore $(cert) -storepass $(cert_pswd) $(apk) $(app_xml) $(swf) -extdir .
#	$(adt) -package -target apk-captive-runtime -storetype pkcs12 -keystore $(cert) -storepass $(cert_pswd) $(apk) $(app_xml) $(swf) -extdir .

$(ipa): $(ios_ane) $(cert) $(swf) $(app_xml)
ifeq ($(OS),Darwin)
#	$(adt) -package -target  ipa-test-interpreter-simulator -storetype pkcs12 -keystore $(dev_p12) -storepass $(ios_pswd) $(ipa) $(app_xml) $(swf) -platformsdk $(ios_simulator_sdk) -extdir .
#	$(adt) -installApp -platform ios -platformsdk $(ios_simulator_sdk) -device ios-simulator -package $(ipa)
#	$(adt) -launchApp -platform ios -platformsdk $(ios_simulator_sdk) -device ios-simulator -appid $(app_id)
endif
#	$(adt) -package -target ipa-app-store -useLegacyAOT no -storetype pkcs12 -keystore $(dev_p12) -storepass $(ios_pswd) -provisioning-profile $(dev_mobileprovision) $(ipa) $(app_xml) $(swf) -extdir .
	$(adt) -package -target ipa-test-interpreter -storetype pkcs12 -keystore $(dev_p12) -storepass $(ios_pswd) -provisioning-profile $(dev_mobileprovision) $(ipa) $(app_xml) $(swf) -extdir .


$(cert):
	$(adt) -certificate -validityPeriod 25 -cn SelfSigned 2048-RSA $(cert) $(cert_pswd)
#	openssl genrsa -out mykey.key 2048
#	openssl req -new -key mykey.key -out CertificateSigningRequest.certSigningRequest  -subj "/emailAddress=db0@qq.com, CN=Biao Li, C=CN"
#	openssl x509 -in ios_distribution.cer -inform DER -out developer_identity.pem -outform PEM
#	openssl pkcs12 -export -inkey mykey.key -in developer_identity.pem -out iphone_dev.p12

.PHONY : clean
clean:
	rm -rf $(ane_swc) *.ipa *.air *.apk *.exe *.ane *.xml *.swf *.dll *.o *.so *.a *.tmp *.dSYM













	


